<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="重置密码" tabindex="-1"><a class="header-anchor" href="#重置密码"><span>重置密码</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a>
<ul>
<li><a href="#model-preparation">模型准备</a></li>
<li><a href="#database-preparation">数据库准备</a></li>
<li><a href="#configuring-trusted-hosts">配置可信主机</a></li>
</ul>
</li>
<li><a href="#routing">路由</a>
<ul>
<li><a href="#requesting-the-password-reset-link">请求重置密码链接</a></li>
<li><a href="#resetting-the-password">重置密码</a></li>
</ul>
</li>
<li><a href="#deleting-expired-tokens">删除过期令牌</a></li>
<li><a href="#password-customization">个性化</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>大多数Web运用都为用户提供重置和忘记密码的方法。Laravel已经便捷的服务来提供发送密码重置链接和安全重置密码的，而不需要您为每个运用程序重新实现此功能。</p>
<blockquote>
<p>[!注意]<br>
想快速上手吗？ 在全新的Laravel运用程序中安装Laravel运用程序<a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">入门套件</a>。 Laravel的入门套件将负责为您的整个身份验证系统包括重置和忘记密码提供支持。</p>
</blockquote>
<h3 id="模型准备" tabindex="-1"><a class="header-anchor" href="#模型准备"><span>模型准备</span></a></h3>
<p>在使用Laravel的密码重置功能之前，运用程序的 <code v-pre>App\Models\User</code> 模型必须使用 <code v-pre>Illuminate\Notifications\Notifiable</code> trait。通常，在新创建的Laravel运用程序的 <code v-pre>App\Models\User</code> 模型中默认引入了该trait。</p>
<p>接下来， 验证 <code v-pre>App\Models\User</code> 是否实现了 <code v-pre>Illuminate\Contracts\Auth\CanResetPassword</code> 协定。 框架附带的 <code v-pre>App\Models\User</code> 模型已实现此接口，并使用 <code v-pre>Illuminate\Auth\Passwords\CanResetPassword</code> 特征来包含实现接口所需的方法。</p>
<h3 id="数据库准备" tabindex="-1"><a class="header-anchor" href="#数据库准备"><span>数据库准备</span></a></h3>
<p>必须创建一个表来存储运用程序的密码重置令牌。通常，这个包含在Laravel默认的 <code v-pre>0001_01_01_000000_create_users_table.php</code> 数据库迁移中。</p>
<h3 id="配置可信主机" tabindex="-1"><a class="header-anchor" href="#配置可信主机"><span>配置可信主机</span></a></h3>
<p>默认情况下， 无论HTTP请求的<code v-pre>Host</code>头的内容是什么，Laravel都会响应它收到的所有请求。此外，在Web请求期间生成运用程序的绝对URL时，将使用 <code v-pre>Host</code> 标头的值。</p>
<p>通常，您应该将Web服务器（如Nginx或Apache）配置为仅向运用程序发送与给定主机名匹配的请求。但是，如果您无法直接自定义Web服务器，并且需要提示Laravel仅响应某些主机名，则可以使用运用程序的<code v-pre>bootstrap/app.php</code>文件中的 <code v-pre>trustHosts</code> 中间件方法来实现。当运用程序提供密码重置功能时，这一点尤为重要。</p>
<p>要了解有关此中间件的更多信息，请参阅 <a href="https://learnku.com/docs/laravel/11.x/requests#configuring-trusted-hosts" target="_blank" rel="noopener noreferrer"><code v-pre>TrustHosts</code> 中间件文档</a>.</p>
<h2 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h2>
<p>为了正确实现允许用户重置密码的支持，我们需要定义几个路由。首先，我们需要一对路由来处理，允许用户通过他们电子邮件地址请求密码重置链接。其次，一旦用户访问通过电子邮件发送给他们的密码重置链接并完成密码重置表单，我们将需要一对路由来处理实际重置密码。</p>
<h3 id="请求重置密码链接" tabindex="-1"><a class="header-anchor" href="#请求重置密码链接"><span>请求重置密码链接</span></a></h3>
<h4 id="密码重置链接申请表" tabindex="-1"><a class="header-anchor" href="#密码重置链接申请表"><span>密码重置链接申请表</span></a></h4>
<p>首先，我们将定义请求密码重置链接所需要的路由。然后，我们将定义一个路由，该路由返回一个带有密码重置链接请求表单的视图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/forgot-password'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.forgot-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password.request'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此路由返回的视图应具有包含 <code v-pre>email</code> 字段，该字段将允许用户请求给定电子邮件地址的密码重置链接。</p>
<h4 id="处理表单请求" tabindex="-1"><a class="header-anchor" href="#处理表单请求"><span>处理表单请求</span></a></h4>
<p>接下来，我们将定义一个路由，用于处理来自“忘记密码”视图的表单提交请求。此路由将负责验证电子邮件地址并向相应用户发送密码重置请求:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/forgot-password'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$status</span> <span class="token operator">=</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">sendResetLink</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$status</span> <span class="token operator">===</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token constant">RESET_LINK_SENT</span></span>
<span class="line">                <span class="token operator">?</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token punctuation">:</span> <span class="token class-name return-type">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password.email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在继续之前，让我们更详细的检查这条路由。首先，验证请求的 <code v-pre>email</code> 属性。 接下来， 我们将使用Laravel内置的“密码代理”(通过密码外观) 向用户发送密码重置链接。密码代理将负责通过给定字段 (在本例中为电子邮件地址) 检索用户，并通过Laravel内置的<a href="https://learnku.com/docs/laravel/11.x/notifications" target="_blank" rel="noopener noreferrer">消息通知系统</a>向用户发送密码重置链接。</p>
<p>该 <code v-pre>sendResetLink</code> 方法将返回一个状态标识。可以使用Laravel的 <a href="https://learnku.com/docs/laravel/11.x/localization" target="_blank" rel="noopener noreferrer">本地化</a> 助手来转化此状态，以便向用户显示有关请求状态的用户友好消息。密码重置状态的转换由运用程序的 <code v-pre>lang/{lang}/passwords.php</code> 语言文件来决定。 状态字段的每个可能值的条目位于<code v-pre>passwords</code>语言文件中。</p>
<blockquote>
<p>[!注意]<br>
默认情况下，Laravel运用程序的矿建不包含 <code v-pre>lang</code> 目录。 如果你想定制Laravel的语言文件，您可以通过 Artisan命令的<code v-pre>lang:publish</code>来发布。</p>
</blockquote>
<p>您可能想知道Laravel在调用 <code v-pre>Password</code> facade的 <code v-pre>sendResetLink</code> 方法时如何从运用程序的数据库中检索用户记录。 Laravel密码代理利用身份验证系统的「用户提供者」 来检索数据库记录。密码代理使用用户在<code v-pre>config/auth.php</code>配置文件中配置的<code v-pre>passwords</code>数组。若要了解有关编写自定义用户提供者程序的详细信息，请参阅 <a href="https://learnku.com/docs/laravel/11.x/authentication#adding-custom-user-providers" target="_blank" rel="noopener noreferrer">身份信息验证文档</a>.</p>
<blockquote>
<p>[!注意]<br>
手动实现密码重置时，需要自行定义视图和路由的内容。如果您想要包含所有必要的身份验证和验证逻辑的基架，请参阅<a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">Laravel入门工具包</a>.</p>
</blockquote>
<h3 id="重置密码-1" tabindex="-1"><a class="header-anchor" href="#重置密码-1"><span>重置密码</span></a></h3>
<h4 id="重置密码表单" tabindex="-1"><a class="header-anchor" href="#重置密码表单"><span>重置密码表单</span></a></h4>
<p>接下来，我们将定义在用户单击已通过电子邮件发送给他们的密码重置链接并提供新密码后实际重置密码所需要的路由。首先，让我们定义将显示重置密码表单的路由，该表单在用户单击重置密码链接时显示。此路由将收到一个<code v-pre>token</code>参数，我们稍后将使用该参数来验证密码重置请求:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/reset-password/{token}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$token</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.reset-password'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'token'</span> <span class="token operator">=></span> <span class="token variable">$token</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password.reset'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此路由返回的视图显示一个表单，其中一个包含<code v-pre>email</code> 字段，一个<code v-pre>password</code> 字段，一个 <code v-pre>password_confirmation</code> 字段，和一个隐藏的 <code v-pre>token</code> 字段，其中应包含我们路由接受的机密<code v-pre>$token</code>的值。</p>
<h4 id="处理表单请求-1" tabindex="-1"><a class="header-anchor" href="#处理表单请求-1"><span>处理表单请求</span></a></h4>
<p>当然，我们需要定义一个路由来实际处理密码重置表单提交。此路由将负责验证传入的请求并更新数据库中的用户密码:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>PasswordReset</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Hash</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/reset-password'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'token'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|email'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|min:8|confirmed'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$status</span> <span class="token operator">=</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">reset</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password_confirmation'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'token'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$password</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">forceFill</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$password</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">setRememberToken</span><span class="token punctuation">(</span><span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token function">event</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">PasswordReset</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$status</span> <span class="token operator">===</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token constant">PASSWORD_RESET</span></span>
<span class="line">                <span class="token operator">?</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'login'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token punctuation">:</span> <span class="token class-name return-type">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token function">__</span><span class="token punctuation">(</span><span class="token variable">$status</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guest'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password.update'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>继续之前，让我们更详细的研究一下这个路由。首先，验证请求的 <code v-pre>token</code>，<code v-pre>email</code>， 和 <code v-pre>password</code> 字段。 接下来， 我们将使用Laravel的内置 「密码代理」 (通过<code v-pre>Password</code>Facade) 来验证密码重置请求凭据。</p>
<p>如果提供给密码代理的令牌，电子邮件地址和密码有效， <code v-pre>reset</code> 则将调用传递给该方法的闭包。在这个闭包中，我们可以更新数据库中的用户密码，该必要接收用户实例和提供给密码重置表单的纯文本密码。</p>
<p>该方法返回一个「状态」标识。可以使用Laravel的 <a href="https://learnku.com/docs/laravel/11.x/localization" target="_blank" rel="noopener noreferrer">本地化</a> <code v-pre>reset</code>助手翻译此状态，以便向用户显示有关其请求状态的用户友好消息。密码重置状态的翻译由运用程序的语言文件<code v-pre>lang/{lang}/passwords.php</code>决定，状态标识每个可能值的条目位于<code v-pre>passwords</code>语言文件决定。如果您的运用冲虚没有 <code v-pre>lang</code> 文件夹， 您可以使用Artisan的<code v-pre>lang:publish</code>命令来创建。</p>
<p>在继续之前， 你可能想知道Laravel在调用 <code v-pre>Password</code> facade的 <code v-pre>reset</code> 方法时如何从运用程序数据库中检索用户记录。LaravelMiami代理利用您的身份验证系统的「用户提供者」 来检索数据库记录。密码代理使用用户提供程序在配置文件 <code v-pre>config/auth.php</code> 中的 <code v-pre>passwords</code> 配置数组中的配置。 要了解有关编写自定义用户提供程序的更多信息，请参阅 <a href="https://learnku.com/docs/laravel/11.x/authentication#adding-custom-user-providers" target="_blank" rel="noopener noreferrer">身份验证文档</a>.</p>
<h2 id="删除过期令牌" tabindex="-1"><a class="header-anchor" href="#删除过期令牌"><span>删除过期令牌</span></a></h2>
<p>已过期的密码重置令牌仍在您的数据库中。但是，您可以使用 Artisan 中的 <code v-pre>auth:clear-resets</code> 命令轻松删除这些记录:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan auth:clear-resets</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您想自动化这个过程，请考虑命令增加到您的运用程序的 <a href="https://learnku.com/docs/laravel/11.x/scheduling" target="_blank" rel="noopener noreferrer">任务调度</a>中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth:clear-resets'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyFifteenMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="定制" tabindex="-1"><a class="header-anchor" href="#定制"><span>定制</span></a></h2>
<h4 id="重置链接自定义" tabindex="-1"><a class="header-anchor" href="#重置链接自定义"><span>重置链接自定义</span></a></h4>
<p>您可以使用 <code v-pre>ResetPassword</code> 通知类提供的 <code v-pre>createUrlUsing</code> 方法自定义密码重置链接URL。此方法接受一个闭包，该闭包接收正在接收通知的用户实例以及密码重置链接令牌。通常，您应该从 <code v-pre>App\Providers\AppServiceProvider</code> 服务提供的 <code v-pre>boot</code> 方法调用此方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>ResetPassword</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Bootstrap any application services.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">ResetPassword</span><span class="token operator">::</span><span class="token function">createUrlUsing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$token</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'https://example.com/reset-password?token='</span><span class="token operator">.</span><span class="token variable">$token</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="重置邮件自定义" tabindex="-1"><a class="header-anchor" href="#重置邮件自定义"><span>重置邮件自定义</span></a></h4>
<p>你可以轻松修改用于向用户发送密码重置链接的通知类。 首先，覆盖你的 <code v-pre>App\Models\User</code> 模型上的 <code v-pre>sendPasswordResetNotification</code> 方法。 在此方法中，你可以使用你自己创建的任何 <a href="https://learnku.com/docs/laravel/11.x/notificationsmd/16683" target="_blank" rel="noopener noreferrer">消息通知类</a> 发送通知。 密码重置 <code v-pre>$token</code> 是该方法收到的第一个参数。 你可以使用这个 <code v-pre>$token</code> 来构建你选择的密码重置 URL 并将你的通知发送给用户：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>ResetPasswordNotification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 发送密码重置通知给用户.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$token</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">sendPasswordResetNotification</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'https://example.com/reset-password?token='</span><span class="token operator">.</span><span class="token variable">$token</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ResetPasswordNotification</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/passwordsmd/16695" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pa...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/passwordsmd/16695" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pa...</a></p>
</blockquote>
</div></template>


